package com.cango.qutz.web.util;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * ClassName: UtilHelper
 * </p>
 * <p>
 * Description: 帮助类
 * </p>
 * <p>
 * Author: wzx
 * </p>
 * <p>
 * Date: 2015-05-14
 * </p>
 */
public class UtilHelper {

    /**
     * <p>
     * Description: 判断是否为非空
     * </p>
     *
     * @param obj 对象
     * @return true/false
     */
    public static boolean notNull(Object obj) {
        return (obj != null) && (!"".equals(obj.toString()));
    }

    /**
     * <p>
     * Description: 判断是否为空
     * </p>
     *
     * @param obj 对象
     * @return true/false
     */
    public static boolean isNull(Object obj) {
        if ((obj != null) && (!"".equals(obj))) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 得到用户访问的真实IP地址
     * @param request 访问对象
     * @return ip地址
     */
    public static String getRealIP(HttpServletRequest request) {
        if (UtilHelper.notNull(request)) {
            //非代理
            if (UtilHelper.isNull(request.getHeader("x-forwarded-for"))) {
                return request.getRemoteAddr();
            } else {
                return request.getHeader("x-forwarded-for");
            }
        } else {
            return null;
        }
    }
}
